<?php namespace App\Http\Services;

use App\Admin;
use Carbon\Carbon;
use Auth;

class AdminManager
{
    /**
     * 检查用户名是否已经存在或被使用
     * @param $username
     * @return bool
     */
    public static function isUsernameExists($username){
        if(Admin::whereUsername($username)->exists())
            return true;
        return false;
    }

    /**
     * 检查email是否已经存在或被使用
     * @param $email
     * @return bool
     */
    public static function isEmailExists($email){
        if(Admin::whereEmail($email)->exists())
            return true;
        return false;
    }

    /**
     * 通过用户名来登录
     * @param $username
     * @param $password
     * @param bool $remeber
     */
    public static function signInByUsername($username, $password, $remember = false){
        if(!self::isUsernameExists($username))
            return false;

        if(Auth::guard('admin')->attempt(['username' => $username, 'password' => $password],$remember)){
            return Auth::guard('admin')->user();
        }

        return false;
    }
}